<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="100  px">
      <el-form-item label="任务id" prop="taskID">
        <el-input
          v-model="queryParams.taskID"
          placeholder="请输入任务id"
          clearable
          class="home_input"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="产品编号" prop="productID">
        <el-input
          v-model="queryParams.productID"
          placeholder="请输入产品编号"
          clearable
          class="home_input"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="型号" prop="model">
        <el-input
          v-model="queryParams.model"
          placeholder="请输入型号"
          clearable
          class="home_input"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="送检单位" prop="offer">
        <el-input
          v-model="queryParams.offer"
          placeholder="请输入送检单位"
          clearable
          class="home_input"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="生产厂家" prop="mfr">
        <el-input
          v-model="queryParams.mfr"
          placeholder="请输入生产厂家"
          clearable
          class="home_input"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="承雨口直径(mm)" prop="diamdterStd">
        <el-input
          v-model="queryParams.diamdterStd"
          placeholder="请输入承雨口直径(mm)"
          clearable
          class="home_input"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="分辨率(mm/斗)" prop="resolution">
        <el-input
          v-model="queryParams.resolution"
          placeholder="请输入分辨率(mm/斗)"
          clearable
          class="home_input"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button size="mini"
                   @click="handleQuery">查询
        </el-button>
        <el-button size="mini"
                   @click="resetQuery">重置
        </el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"

        >新增
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"

        >修改
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"

        >删除
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"

        >导出
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="raintestrecordList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"/>

      <el-table-column label="任务id" align="center" prop="taskID"/>
      <el-table-column label="产品编号" align="center" prop="productID"/>
      <el-table-column label="雨量计类型" align="center" prop="type" :formatter="typeFilter"/>
      <el-table-column label="型号" align="center" prop="model"/>
      <el-table-column label="送检单位" align="center" prop="offer"/>
      <el-table-column label="生产厂家" align="center" prop="mfr"/>
      <el-table-column label="承雨口直径(mm)" align="center" prop="diamdterStd"/>
      <el-table-column label="分辨率(mm/斗)" align="center" prop="resolution"/>

      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"

          >修改
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"

          >删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改雨量计任务对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="800px" append-to-body>

      <el-form ref="form" :model="form" :rules="rules" label-width="120px">
        <el-row>
          <el-col :span="12">
            <el-form-item label="任务id" prop="taskID">
              <el-input v-model="form.taskID" placeholder="请输入任务id"/>
            </el-form-item>
            <el-form-item label="产品编号" prop="productID">
              <el-input v-model="form.productID" placeholder="请输入产品编号"/>
            </el-form-item>
            <el-form-item label="型号" prop="model">
              <el-input v-model="form.model" placeholder="请输入型号"/>
            </el-form-item>
            <el-form-item label="送检单位" prop="offer">
              <el-input v-model="form.offer" placeholder="请输入送检单位"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="生产厂家" prop="mfr">
              <el-input v-model="form.mfr" placeholder="请输入生产厂家" style="width: 210px;"/>
            </el-form-item>
            <el-form-item label="承雨口直径" prop="diamdterStd">
              <el-input v-model="form.diamdterStd" placeholder="请输入承雨口直径" style="width: 180px;"/>
              <span style="margin-left: 5px;">(mm)</span>
            </el-form-item>
            <el-form-item label="分辨率(mm/斗)" prop="resolution">
              <el-select v-model="form.resolution" placeholder="请选择分辨率(mm/斗)">
                <el-option
                  v-for="item in resolutionList"
                  :key="item.value"
                  :label="item.value"
                  :value="item.label">
                </el-option>
              </el-select>

            </el-form-item>
            <el-form-item label="雨量计类型" prop="type">
              <el-select v-model="form.type" placeholder="请选择仪器类别">
                <el-option
                  v-for="item in typeList"
                  :key="item.value"
                  :label="item.value"
                  :value="item.label">
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
  import {
    listRaintestrecord,
    getRaintestrecord,
    delRaintestrecord,
    addRaintestrecord,
    updateRaintestrecord
  } from "@/api/system/raintestrecord";

  export default {
    name: "Raintestrecord",
    data() {
      return {
        // 遮罩层
        loading: true,
        // 选中数组
        ids: [],
        // 非单个禁用
        single: true,
        // 非多个禁用
        multiple: true,
        // 显示搜索条件
        showSearch: true,
        // 总条数
        total: 0,
        // 雨量计任务表格数据
        raintestrecordList: [],
        // 弹出层标题
        title: "",
        // 是否显示弹出层
        open: false,
        resolutionList: [
          {
            label: '0.1',
            value: '0.1',
          },
          {
            label: '0.2',
            value: '0.2',
          },
          {
            label: '0.5',
            value: '0.5',
          },
          {
            label: '1.0',
            value: '1.0',
          },

        ],
        typeList: [
          {
            label: '1',
            value: '翻斗式雨量计',
          },
          {
            label: '2',
            value: '称重式雨量计',
          },
          {
            label: '3',
            value: '虹吸式雨量计',
          },
          {
            label: '4',
            value: '压电雨量计',
          },
          {
            label: '5',
            value: '光学雨量计',
          },
        ],
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 10,
          taskID: null,
          productID: null,
          type: null,
          model: null,
          offer: null,
          mfr: null,
          diamdterStd: null,
          resolution: null,
          TestState: null,
          StationNumber: null,
          TestRegID: null,
          TestRegName: null,
          Testtype: null,
          TestItems: null,
          TestItemsResult: null,
          TestIntensity: null,
          TestRainfall: null,
          TestRepeat: null,
          Temperature: null,
          Humidity: null,
          Atmos: null,
          DiamdterMeasure: null,
          AngleMeasure: null,
          DepthMeasure: null,
          Point1M0: null,
          Point2M0: null,
          Point3M0: null,
          Point1M1: null,
          Point2M1: null,
          Point3M1: null,
          Point1T: null,
          Point2T: null,
          Point3T: null,
          Point1Tips: null,
          Point2Tips: null,
          Point3Tips: null,
          Point1Time: null,
          Point2Time: null,
          Point3Time: null,
          TestDate: null,
          ExpirationDate: null
        },
        // 表单参数
        form: {},
        // 表单校验
        rules: {
          taskID: [
            {required: true, message: "任务id不能为空", trigger: "blur"}
          ],
          productID: [
            {required: true, message: "产品编号不能为空", trigger: "blur"}
          ],
          model: [
            {required: true, message: "型号不能为空", trigger: "blur"}
          ],
          mfr: [
            {required: true, message: "生产厂家不能为空", trigger: "blur"}
          ],
          diamdterStd: [
            {required: true, message: "承雨口直径(mm)不能为空", trigger: "blur"}
          ],
          resolution: [
            {required: true, message: "分辨率(mm/斗)不能为空", trigger: "blur"}
          ],
          type: [
            {required: true, message: "分辨率(mm/斗)不能为空", trigger: "change"}
          ],
        }
      };
    },
    created() {
      this.getList();
    },
    methods: {
      /** 查询雨量计任务列表 */
      getList() {
        this.loading = true;
        listRaintestrecord(this.queryParams).then(response => {
          this.raintestrecordList = response.rows;
          this.total = response.total;
          this.loading = false;
        });
      },
      // 取消按钮
      cancel() {
        this.open = false;
        this.reset();
      },
      // 表单重置
      reset() {
        this.form = {
          registerID: null,
          taskID: null,
          productID: null,
          type: null,
          model: null,
          offer: null,
          mfr: null,
          diamdterStd: null,
          resolution: null,
          TestState: null,
          StationNumber: null,
          TestRegID: null,
          TestRegName: null,
          Testtype: null,
          TestItems: null,
          TestItemsResult: null,
          TestIntensity: null,
          TestRainfall: null,
          TestRepeat: null,
          Temperature: null,
          Humidity: null,
          Atmos: null,
          DiamdterMeasure: null,
          AngleMeasure: null,
          DepthMeasure: null,
          Point1M0: null,
          Point2M0: null,
          Point3M0: null,
          Point1M1: null,
          Point2M1: null,
          Point3M1: null,
          Point1T: null,
          Point2T: null,
          Point3T: null,
          Point1Tips: null,
          Point2Tips: null,
          Point3Tips: null,
          Point1Time: null,
          Point2Time: null,
          Point3Time: null,
          TestDate: null,
          ExpirationDate: null
        };
        this.resetForm("form");
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.pageNum = 1;
        this.getList();
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.resetForm("queryForm");
        this.handleQuery();
      },
      // 多选框选中数据
      handleSelectionChange(selection) {
        this.ids = selection.map(item => item.registerID)
        this.single = selection.length !== 1
        this.multiple = !selection.length
      },
      /** 新增按钮操作 */
      handleAdd() {
        this.reset();
        this.open = true;
        this.title = "添加雨量计任务";
      },
      /** 修改按钮操作 */
      handleUpdate(row) {
        this.reset();
        const registerID = row.registerID || this.ids
        getRaintestrecord(registerID).then(response => {
          this.form = response.data;
          this.open = true;
          this.title = "修改雨量计任务";
        });
      },
      typeFilter(row) {
        switch (row.type) {
          case '1':
            return '翻斗式雨量计';
            break;
          case '2':
            return '称重式雨量计';
            break;
          case '3':
            return '虹吸式雨量计';
          case '4':
            return '压电雨量计';
          case '5':
            return '光学雨量计';
            break;
        }
      },
      /** 提交按钮 */
      submitForm() {
        this.$refs["form"].validate(valid => {
          if (valid) {
            if (this.form.registerID != null) {
              updateRaintestrecord(this.form).then(response => {
                this.$modal.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              });
            } else {
              addRaintestrecord(this.form).then(response => {
                this.$modal.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              });
            }
          }
        });
      },
      /** 删除按钮操作 */
      handleDelete(row) {
        const registerIDs = row.registerID || this.ids;
        this.$modal.confirm('是否确认删除雨量计任务编号为"' + registerIDs + '"的数据项？').then(function () {
          return delRaintestrecord(registerIDs);
        }).then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        }).catch(() => {
        });
      },
      /** 导出按钮操作 */
      handleExport() {
        this.download('system/raintestrecord/export', {
          ...this.queryParams
        }, `raintestrecord_${new Date().getTime()}.xlsx`)
      }
    }
  };
</script>
